(function (p) { /* Sai.Calendar.Grid */

    var Grid = function (container) {

        this.container  = container;
        this.days       = container.find("table td > div");
        this.selectedDay;
        
        this.days.click($.proxy(this.onDayClick, this));
        
        this.days.find("button.day").click($.proxy(this.onDayNumberClick, this));

    }
    Grid.fn = Grid.prototype;


    EventDispatcher.prototype.apply(Grid.prototype);


    Grid.fn.setData = function ( data ) {

        this.data = data;

    }


    Grid.fn.setCurrentDate = function ( year, month ) {
        
        this.currentDate        = new Date(year, month);
        
        var previousMonthDate   = new Date(year, month - 1);
        var previousMonthDays   = (new Date(year, month, 0)).getDate();
        var currentDay          = this.currentDate.getDay();
        var currentMonthDays    = (new Date(year, month+1, 0)).getDate();
        var day;
        var dayInPreviousMonth;
        var dayInCurrentMonth;
        var dayInNextMonth;
        var event;
        var forDate;
        
        /*
        console.log("Grid.fn.setCurrentDate:");
        console.log("currentDay:" + currentDay);
        console.log("currentMonthDays:" + currentMonthDays);
        */
        for(var i=0; i < this.days.length; i++){
            
            day = $(this.days[i]);
            day.removeClass("today");
            day.removeClass("out");
            day.removeClass("selected");
            day.children("button").text("");
            day.children("button.event").remove();
            
            if( i < currentDay ){ /* Mes anterior */
                
                dayInPreviousMonth = previousMonthDays - (currentDay - (previousMonthDays - (previousMonthDays - i) + 1));
                day.children("button").text(dayInPreviousMonth);
                
                day.addClass("out");
                
                forDate = new Date(year, month - 1, dayInPreviousMonth);
                
            }else if ( i >= currentDay && i - currentDay < currentMonthDays){ /* Mes seleccionado */
                
                dayInCurrentMonth = (i - currentDay + 1);
                
                if( i == currentDay ){
                    
                    day.children("button").text(dayInCurrentMonth + " de " + Sai.Calendar.MONTHS_SHORT[this.currentDate.getMonth()]);
                    
                }else{
                    
                    day.children("button").text(dayInCurrentMonth);
                    
                }
                
                if( year == Sai.Calendar.TODAY.getFullYear() && 
                    month == Sai.Calendar.TODAY.getMonth() && 
                    dayInCurrentMonth == Sai.Calendar.TODAY.getDate() ) {
                    
                    day.addClass("today");
                    
                }
                
                forDate = new Date(year, month, dayInCurrentMonth);
                
            }else if( i > currentMonthDays ){ /* Mes siguiente */
                
                dayInNextMonth = (i - currentDay - currentMonthDays + 1);
                
                day.addClass("out");
                
                if( dayInNextMonth == 1 ) {
                    
                    day.children("button").text(dayInNextMonth + " de " + Sai.Calendar.MONTHS_SHORT[this.currentDate.getMonth() + 1]);
                    
                }else{
                    
                    day.children("button").text(dayInNextMonth);
                    
                }
                
                forDate = new Date(year, month + 1, dayInNextMonth);
                
            }
            
            
            for(var j=0; j < this.data.length; j++){
                
                var event   = this.data[j];
                var date    = new Date(Number(event.date));
                
                //console.log(forDate.getFullYear() + "  " + date.getFullYear());
                //console.log(forDate.getMonth() + "  " + date.getMonth());
                //console.log(forDate.getDate() + "  " + date.getDate());
                //console.log("");
                
                if( forDate.getFullYear() == date.getFullYear() && 
                    forDate.getMonth() == date.getMonth() && 
                    forDate.getDate() == date.getDate() ) {
                    
                    //console.log("caca"+event);
                    var button = this.createEvent( event );
                        button.click($.proxy(this.onEventClick, this));
                    
                    day.append(button);
                    
                }
                
            }
            
        }

    }


    Grid.fn.createEvent = function ( data ) {
    
        var id      = data.id;
        var date    = new Date(Number(data.date));
        var hour    = Sai.Calendar.Utils.formatSchedule(date.getHours(), date.getMinutes());
        var type    = data.type;
        var name    = data.name;
        
        var button = $([
            '<button type="button" class="event" data-id="' + id + '" data-type="' + type + '">',
                '<span class="icon star"></span>',
                '<span class="icon globe"></span>',
                '<span class="icon flag"></span>',
                '<span class="icon inbox"></span>',
                '<span class="icon user"></span>',
                hour + ' ' + name,
            '</button>'
        ].join(""));
        
        return button;

    }


    Grid.fn.onDayClick = function ( event ) {
        
        this.selectedDay = $(event.currentTarget);
        
        this.days.removeClass("selected");
        
        this.selectedDay.addClass("selected");

    }


    Grid.fn.onDayNumberClick = function ( event ) {
        
        this.selectedDay = $(event.currentTarget);

    }


    Grid.fn.onEventClick = function ( event ) {
        
        this.selectedEvent = $(event.currentTarget);
        
        this.days.find("button.event").removeClass("selected");
        
        this.selectedEvent.addClass("selected");

    }
    
    Grid.OPEN_DAY = "onOpenDay";

    p.Grid = Grid;

})(Sai.Calendar);